package com.atguigu.crowd.mvc.handler;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.atguigu.crowd.constant.CrowdConstant;
import com.atguigu.crowd.entity.Admin;
import com.atguigu.crowd.service.api.AdminService;
import com.atguigu.crowd.util.ResultEntity;
import com.github.pagehelper.PageInfo;

@Controller
public class AdminHandler {

	@Autowired
	private AdminService adminService;
	
	//登录
	@RequestMapping("/admin/do/login.html")
	public String doLogin(@RequestParam("loginAcct") String loginAcct,
			@RequestParam("userPswd") String userPswd,
			HttpSession session) {
		Admin admin = adminService.getAdminByLoginAcctAndUserPwd(loginAcct,userPswd);
		session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN, admin);
		return "redirect:/admin/to/main/page.html";
	}
	
	//退出
	@RequestMapping("/admin/do/logout.html")
	public String doLogout(HttpSession session) {
		session.invalidate();
		return "redirect:/admin/to/login/page.html";
	}
	
	//分页查询
	@RequestMapping("/admin/get/page.html")
	public String getPageInfo(
			@RequestParam(value="pageNum",defaultValue="1") Integer pageNum,
			@RequestParam(value="pageSize",defaultValue="5") Integer pageSize,
			@RequestParam(value="keyword",defaultValue="") String keyword,
			ModelMap modelMap
			) {
		PageInfo<Admin> pageInfo = adminService.getPageInfo(pageNum, pageSize, keyword);
		modelMap.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO, pageInfo);
		modelMap.addAttribute("keyword", keyword);
		
		return "admin-page";
		
	}
	//添加admin
	@PreAuthorize("hasAuthority('user:add')")
	@RequestMapping("/admin/save.html")
	public String saveAdmin(Admin admin) {
		adminService.saveAdmin(admin);
		return "redirect:/admin/get/page.html?pageNum="+Integer.MAX_VALUE;
	}
	
	//查看admin详情
	@PreAuthorize("hasAuthority('user:get')")
	@RequestMapping("/admin/getAdmin/page.html")
	public String getAdmin(@RequestParam("adminId") Integer adminId,ModelMap modelMap) {
		Admin admin = adminService.getAdminById(adminId);
		modelMap.addAttribute("admin", admin);
		return "admin-search";
	}
	
	//跳转编辑页面
	@PreAuthorize("hasAuthority('user:update')")
	@RequestMapping("/admin/to/edit/page.html")
	public String editUI(
			@RequestParam("adminId") Integer adminId,
			@RequestParam("pageNum") Integer pageNum,
			ModelMap modelMap
			) {
		
		Admin admin = adminService.getAdminById(adminId);
		modelMap.addAttribute("admin", admin);
		return "admin-edit";
	}
	
	//编辑admin
	@PreAuthorize("hasAuthority('user:update')")
	@RequestMapping("/admin/update.html")
	public String update(Admin admin,@RequestParam("pageNum") Integer pageNum) {
		adminService.update(admin);
		return "redirect:/admin/get/page.html?pageNum="+pageNum;
	}
	
	//单个删除
	@PreAuthorize("hasAuthority('user:delete')")
	@RequestMapping("/admin/remove/{adminId}/{pageNum}.html")
	public String remove(
			@PathVariable("adminId") Integer adminId,
			@PathVariable("pageNum") Integer pageNum
			) {
		adminService.remove(adminId);
		return "redirect:/admin/get/page.html?pageNum="+pageNum;
	}
	
	//批量删除
	@PreAuthorize("hasAuthority('user:delete')")
	@ResponseBody
	@RequestMapping("/admin/remove/by/id/array.json")
	public ResultEntity<String> batchRemove(@RequestBody List<Integer> ids){
		
		adminService.batchRemove(ids);
		return ResultEntity.successWithoutData();
	}
}
